package com.example.studentinfo;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;

public class DbHelper extends SQLiteOpenHelper {
    String create_table="CREATE TABLE Student (_id integer primary key autoincrement,xm text,xb text,xh text,bj text,zy text,cj integer)";
    //创建student表
    Context context;

    /**
     * 构造函数
     * @param context 上下文
     * @param dbname 数据库名称
     * @param version 数据库版本
     */
    public DbHelper(Context context,String dbname,int version){
        super(context,dbname,null,version);
        this.context=context;//上下文
    }

    /**
     * 数据库第一次被创建时调用该方法
     * @param db
     */
    @Override
    public void onCreate(SQLiteDatabase db) {db.execSQL(create_table);}

    /**
     * 当数据库版本号增加是被调用
     * @param db
     * @param oldVersion
     * @param newVersion
     */
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
    // 对表进行数据的插入方法
    public void insert(String tableName, ContentValues values){
        SQLiteDatabase db = getWritableDatabase();
        db.insert(tableName,null,values);
        Toast.makeText(context,"成功插入数据！",Toast.LENGTH_SHORT).show();
    }

    public Cursor queryAll(String tableName){
        SQLiteDatabase db = getReadableDatabase();
        //成绩从高到底排序
        Cursor cursor=db.rawQuery("select * from Student ORDER BY cj DESC;",null);
        System.out.println(cursor.getColumnCount());
        return cursor;
    }
    //对表中的姓名进行查询
    public Cursor queryByStudentXm(String tableName,String xm){
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor=db.rawQuery("select * from Student where xm like '%"+xm+"%'",null);
        //db.close();不能关闭数据连接，否则findbyname中的listView.setAdapter(adapter);会报错
        return cursor;
    }
    //对表中的性别进行查询
    public Cursor queryByStudentXb(String tableName,String xb){
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor=db.rawQuery("select * from Student where xb like '%"+xb+"%'",null);
        //db.close();不能关闭数据连接，否则findbyname中的listView.setAdapter(adapter);会报错
        return cursor;
    }
    //对表中的学号进行查询
    public Cursor queryByStudentXh(String tableName,String xh){
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor=db.rawQuery("select * from Student where xh like '%"+xh+"%'",null);
        //db.close();不能关闭数据连接，否则findbyname中的listView.setAdapter(adapter);会报错
        return cursor;
    }
    //对表中的班级进行查询
    public Cursor queryByStudentBj(String tableName,String bj){
        SQLiteDatabase db = getReadableDatabase();

        Cursor cursor=db.rawQuery("select * from Student where bj like '%"+bj+"%'",null);
        //db.close();不能关闭数据连接，否则findbyname中的listView.setAdapter(adapter);会报错
        return cursor;
    }
    //对表中的专业进行查询
    public Cursor queryByStudentZy(String tableName,String zy){
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor=db.rawQuery("select * from Student where zy like '%"+zy+"%'",null);
        //db.close();不能关闭数据连接，否则findbyname中的listView.setAdapter(adapter);会报错
        return cursor;
    }
    //对表中的成绩进行查询
    public Cursor queryByStudentCj(String tableName,String min,String max){
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor=db.rawQuery("select * from Student where cj BETWEEN "+min+" AND "+max+" ",null);
        //db.close();不能关闭数据连接，否则findbyname中的listView.setAdapter(adapter);会报错
        return cursor;
    }
    //删除表中数据的方法
    public void delStudent(String id){
        SQLiteDatabase db = getWritableDatabase();
        db.delete("Student","_id=?",new String[]{id});
    }
    //对表进行更新
    public void  updateStudent(String id,ContentValues values){
        SQLiteDatabase db = getWritableDatabase();
        db.update("Student",values,"_id=?",new String[]{id});
    }
}